package com.zzptc.zzsgz.bdsjws.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.Toast;

import com.socks.library.KLog;
import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.services.IVersionUpdate;
import com.zzptc.zzsgz.bdsjws.services.MyDownLoadService;
import com.zzptc.zzsgz.bdsjws.utils.MyUtil;
import com.zzptc.zzsgz.bdsjws.vo.VersionInfo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/5/22.
 * 广播接收者 接收方
 */

public class CheckUpdateBroadcast extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, final Intent intent) {
       // KLog.i("---","====我准备做更新了====");
        //访问网络 每隔7天 获取versioncode 使用retrofit2框架访问网络
        SharedPreferences sharedPreferences= MyUtil.getSharedPre();
       String olddate= sharedPreferences.getString("checkDate","2017-5-10");
        Date newdate=new Date();//当前日期
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String newdateformat=sdf.format(newdate);
        //sdf.format();由日期类转成String
        //sdf.parse();将String转成日期类
        try {
            Date checkdate=null;
            if (olddate!=null){
                checkdate=sdf.parse(olddate);
            }
            if (olddate==null||"".equals(olddate)){
                //表示第一次安装 将当前的日期储存在共享首选项
                SharedPreferences.Editor editor=sharedPreferences.edit();
                editor.putString("checkDate",newdateformat);
                editor.commit();


            }else {
                //计算日期差
                int days= MyUtil.daysBetween(checkdate,newdate);
                //Toast.makeText(context,days+ "", Toast.LENGTH_SHORT).show();
                if (days>=7){
                    //访问服务器 看看有没有新的版本号
                    Retrofit retrofit=new Retrofit.Builder()
                            //.baseUrl("http://10.0.2.2:8080/")
                            .baseUrl("http://192.168.1.137:8080/")
                            .addConverterFactory(GsonConverterFactory.create())
                            .build();

                    IVersionUpdate versionUpdate=retrofit.create(IVersionUpdate.class);//创建接口对象
                    Call<VersionInfo> versionInfoCall= versionUpdate.getVersion();
                    //将当前的日期储存在共享首选项 作为上次更新的日期
                    versionInfoCall.enqueue(new Callback<VersionInfo>() {
                        @Override
                        public void onResponse(Call<VersionInfo> call, Response<VersionInfo> response) {
                            VersionInfo versionInfo=response.body();
                            int newversioncode=versionInfo.getVersioncode();
                            int oldeversioncode=MyUtil.getVersionCode(MyApplication.getContext());
                            if (newversioncode>oldeversioncode){
                                //进行强制更新
                                Intent intent1=new Intent();
                                intent1.setClass(MyApplication.getContext(), MyDownLoadService.class);
                                intent1.putExtra("apkurl",versionInfo.getApkurl());
                                MyApplication.getContext().startService(intent1);
                            }
                        }

                        @Override
                        public void onFailure(Call<VersionInfo> call, Throwable t) {

                        }
                    });
                }else {

                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //计算日期差

    }
}
